package com.artifact.dao.impl;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import com.artifact.dao.AdminDao;
import com.artifact.model.Admin;

@Repository("adminDao")
public class AdminDaoImpl implements AdminDao {
	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public boolean addAdmin(String userid, String password, String remarksInfo) {
		String sql = "INSERT INTO `yungou_assist`.`admin` (`userid`, `password`,`remarksInfo`) VALUES (?,?,?)";
		return jdbcTemplate.update(sql, userid, password, remarksInfo) > 0;
	}

	@Override
	public Admin getAdmin(String userid, String password) {
		String sql = "select * from admin where userid = ? and password = ?";
		try {
			return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Admin>(Admin.class), userid, password);
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public Admin getAdmin(String userid) {
		String sql = "select * from admin where userid = ?";
		try {
			return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Admin>(Admin.class), userid);
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public List<Admin> getAdmins() {
		String sql = "select * from admin";
		return jdbcTemplate.query(sql, new BeanPropertyRowMapper<Admin>(Admin.class));
	}

	@Override
	public int getAdminListNum() {
		String sql = "select count(id) as id from admin WHERE `userType`=1";
		return jdbcTemplate.queryForObject(sql, Integer.class);
	}

	@Override
	public boolean AdminLogin(String userid, Date loginTime) {
		String sql = "UPDATE admin SET `loginNumber`=loginNumber+1,`loginTime`=? WHERE `userid`=?";
		return jdbcTemplate.update(sql, loginTime, userid) > 0;
	}

	@Override
	public boolean deleteAdmin(String userid) {
		String sql = "DELETE FROM `admin` WHERE `userid`=?";
		return jdbcTemplate.update(sql, userid) > 0;
	}

	@Override
	public boolean updateRemarksInfo(String userid, String remarksInfo) {
		String sql = "UPDATE admin SET `remarksInfo`=? WHERE `userid`=?";
		return jdbcTemplate.update(sql, remarksInfo, userid) > 0;
	}

	@Override
	public boolean updateUserid(String userid, String newUserid) {
		String sql = "UPDATE admin SET `userid`=? WHERE `userid`=?";
		return jdbcTemplate.update(sql, newUserid, userid) > 0;
	}

	@Override
	public boolean updatePassword(String userid, String password) {
		String sql = "UPDATE admin SET `password`=? WHERE `userid`=?";
		return jdbcTemplate.update(sql, password, userid) > 0;
	}

}
